import pandas
import pydicom
import cv2
import numpy as np


def load_PET():
    origin_path = 'D:/lung_cancer/data/all_data.csv'
    data = pandas.read_csv(origin_path)
    for i in range(5):
        patient = data['patientID'][i]
        ct_size = data['ct_size'][i]
        pet_size = data['pet_size'][i]
        ct_spacing = data['ct_x_spacing'][i]
        pet_spacing = data['pet_x_spacing'][i]
        pet_path = data['PET_origin_path'][i]
        cancer_type = data['cancer_type'][i]
        intercept = float(data['pet_intercept'][i])
        slope = float(data['pet_slope'][i])
        slice = pydicom.read_file('H:/'+pet_path)
        pet_array = slice.pixel_array
        print(patient, ':', np.max(pet_array), intercept, slope)

        pet_array = pet_array.astype(np.int32)
        if slope != 1:
            pet_array = slope * pet_array.astype(np.float64)
            pet_array = pet_array.astype(np.int32)
        pet_array += np.int32(intercept)
        pet_array = np.array(pet_array, dtype=np.int32)
        print(patient, ':', np.max(pet_array))
        # real_size = int(round(pet_size*pet_spacing/ct_spacing))
        # border = (real_size-ct_size)//2
        #
        # resized_array = cv2.resize(pet_array, (real_size, real_size))
        # new_pet = resized_array[border:border+ct_size, border:border+ct_size]
        #
        # save_path = 'D:/lung_cancer/data/Slice/PETSlice/'+str(patient)+'_'+str(cancer_type)+'_'+'PETSlice.npy'
        # np.save(save_path, new_pet)
        # print('%d--->patient: %s' % (i+1, str(patient)))


if __name__ == '__main__':
    load_PET()